<?php

namespace app\api\controller;

use app\admin\model\patrol\Inspection;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 商户
 */
class Shop extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\User();
        $this->modelInspection = new Inspection();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 根据用户所在城市进行划分 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];

            $pageArr = array_merge($pageArr, $input);
            $where['istrue'] = 2;
            if (!empty($input['inspection_ids'])){
                $where['patrol_inspection_id'] = ['in',$input['inspection_ids']];
            }
            if (!empty($input['istype'])){
                $where['istype'] = $input['istype'];
            }

            $data = $this->model->where($where)
                ->order('id desc')
                ->field('id,name,license,username,coverimage,status,istype,lnglat')
                ->paginate(['page' => $pageArr['page'] ?: 1, 'list_rows' => $pageArr['page_limit'] ?: 10]);
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }



    /**
     * 问题上报 列表
     * @return void
     */
    public function task_list()
    {
        $input = input();
        $user_info = $this->auth->getUserinfo();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $pageArr = array_merge($pageArr, $input);


            //根据用户所在的管辖
           $inspection_ids = $this->modelInspection->where('user_staff_class_id', $user_info['user_staff_class_id'])
                ->column('id','id');
            $where['patrol_inspection_id'] = ['in' , $inspection_ids];

            if (!empty($input['keyWord'])){
                $where['name'] = ['like','%'.$input['keyWord'].'%'];
            }


            $data = $this->model->where($where)
                ->where('istrue',2)
                ->order('id desc')
                ->field('id,name,license,username,coverimage,status,istype,lnglat')
                ->paginate(['page' => $pageArr['page'] ?: 1, 'list_rows' => $pageArr['page_limit'] ?: 10]);
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}